using ASP;
using HoWave.Common;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Admin;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Admin_Admin_Client_AnnalManage : Admin, IRequiresSessionState
{
	protected ScriptManager ScriptManager1;
	protected Literal ltrTitle;
	protected HtmlTable table_item;
	protected TextBox txtBeginTime;
	protected TextBox txtEndTime;
	protected TextBox txtSearchClientName;
	protected DropDownList ddlSearchRelationType;
	protected DropDownList ddlDate;
	protected Button SeachBut;
	protected GridView gvManage;
	protected UpdatePanel UpdatePanel1;
	protected Literal ltrPagePanel;
	protected Panel pnlManage;
	protected Label lblClientName;
	protected Label lblRelationType;
	protected Label lblContacter;
	protected Label lblRelationTime;
	protected Label lblAnnalContent;
	protected Label lblInputer;
	protected Panel pnlCheckInfo;
	private string linkHead = "";
	private string beforeUrl = "Admin_Client_AnnalManage.aspx?MenuID=2&MenuName=客户管理&FileName=联系记录管理";
	private string actionTab = "";
	private int annalID = 0;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	public string ActionTab
	{
		get
		{
			if (base.Request.QueryString["action"] != null)
			{
				this.actionTab = base.Request.QueryString["action"].Trim();
			}
			else
			{
				this.actionTab = "";
			}
			return this.actionTab;
		}
		set
		{
			this.actionTab = value;
		}
	}
	public int AnnalID
	{
		get
		{
			if (base.Request.QueryString["annalID"] != null)
			{
				this.annalID = Convert.ToInt32(base.Request.QueryString["annalID"].Trim());
			}
			return this.annalID;
		}
		set
		{
			this.annalID = value;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			if (!base.admin.CheckAdminPower(44))
			{
				base.WriteLog("Modidy", Language.GetAdminMsg("Log/Access", "0"), "Admin_Client_AnnalManage");
				base.WriteRightErrMsg();
			}
			this.ShowPanelDisplay(this.ActionTab);
		}
	}
	protected void ShowPanelDisplay(string DisplayName)
	{
		if (DisplayName != null)
		{
			if (DisplayName == "checkinfo")
			{
				this.pnlManage.Visible = false;
				this.pnlCheckInfo.Visible = true;
				if (this.AnnalID > 0)
				{
					this.InitPage();
				}
				return;
			}
		}
		this.pnlManage.Visible = true;
		this.pnlCheckInfo.Visible = false;
		this.ddlSearchRelationType.bindDataToDropDownList("HL_ClientAnnal", "RelationType", false, false);
		this.BindClientAnnal();
	}
	protected void InitPage()
	{
		HoWave.Data.Model.ClientAnnal model = BLLInstance.BllClientAnnal.GetModel(this.AnnalID);
		if (model != null)
		{
			HoWave.Data.Model.Client model2 = HoWave.Data.Bll.Client.instance.GetModel(model.ClientID);
			if (model2 != null)
			{
				string clientName = model2.ClientName;
				this.lblClientName.Text = clientName;
			}
			HoWave.Data.Model.ClientContacter contacterByTrueName = HoWave.Data.Bll.ClientContacter.instance.GetContacterByTrueName(model.Contacter);
			if (contacterByTrueName != null)
			{
				this.lblContacter.Text = model.Contacter;
			}
			this.lblRelationType.Text = this.GetDictionaryTitle(model.RelationType);
			this.lblRelationTime.Text = model.RelationTime.ToString();
			this.lblAnnalContent.Text = model.AnnalContent;
			this.lblInputer.Text = model.Inputer;
		}
	}
	protected string GetDictionaryTitle(string fieldValue)
	{
		string result = "";
		HoWave.Data.Model.Dictionary dictionary = HoWave.Data.Bll.Dictionary.instance.GetDictionary(fieldValue);
		if (dictionary != null)
		{
			result = dictionary.FieldText;
		}
		return result;
	}
	protected void BindClientAnnal()
	{
		string query = this.GetQuery();
		string queryByOrder = this.GetQueryByOrder();
		int num = this.gvManage.PageIndex;
		if (Fetch.Get("pageindex") != null)
		{
			num = Convert.ToInt32(Fetch.GetID("pageindex").ToString());
			if (num == 0)
			{
				num = 1;
			}
		}
		string text = " from HL_Client as c,HL_ClientAnnal as ca where ca.ClientID=c.ClientID ";
		int pageSize = this.gvManage.PageSize;
		ClientByPage clientByPage = new ClientByPage();
		int listCount = clientByPage.GetListCount("select count(*)" + text + query);
		int startIndex = (num - 1) * pageSize + 1;
		int endIndex = num * pageSize;
		string str = "ROW_NUMBER() OVER (" + queryByOrder + ") as row,c.ClientName,(select UserName from HL_User where UserID=c.Owner) as ownerName,ca.* ";
		string strSQL = "select " + str + text + query;
		DataTable listPage = clientByPage.GetListPage(strSQL, "row", startIndex, endIndex);
		this.gvManage.DataSource = listPage;
		this.gvManage.DataBind();
		CurrPageLinks currPageLinks = new CurrPageLinks();
		string url = this.beforeUrl + this.linkHead + "&pageindex";
		this.ltrPagePanel.Text = currPageLinks.CurrPageLink(num, pageSize, listCount, "条记录&nbsp;&nbsp;每页&nbsp;" + pageSize + "&nbsp;条记录", url);
	}
	private string GetQuery()
	{
		string text = "";
		string text2 = Fetch.Get("RelationTime");
		if (!string.IsNullOrEmpty(text2))
		{
			this.ddlDate.ddlSelect(text2);
		}
		else
		{
			text2 = this.ddlDate.SelectedValue.Trim();
		}
		if (!string.IsNullOrEmpty(text2))
		{
			DateTime now = DateTime.Now;
			DateTime dateTime = now.AddDays((double)(1 - Convert.ToInt32(now.DayOfWeek.ToString("d"))));
			DateTime dateTime2 = dateTime.AddDays(6.0);
			string text3 = text2;
			if (text3 != null)
			{
				if (!(text3 == "today"))
				{
					if (!(text3 == "week"))
					{
						if (!(text3 == "weeklast"))
						{
							if (!(text3 == "month"))
							{
								if (text3 == "monthlast")
								{
									text += " and (DATEDIFF(MM,RelationTime,GETDATE())=1)";
								}
							}
							else
							{
								text += " and (DATEDIFF(MM,RelationTime,GETDATE())=0)";
							}
						}
						else
						{
							string text4 = text;
							text = string.Concat(new string[]
							{
								text4,
								" and (RelationTime>='",
								dateTime.AddDays(-7.0).ToString("yyyy-MM-dd"),
								"' and RelationTime<='",
								dateTime2.AddDays(-7.0).ToString("yyyy-MM-dd"),
								"')"
							});
						}
					}
					else
					{
						string text4 = text;
						text = string.Concat(new string[]
						{
							text4,
							" and (RelationTime>='",
							dateTime.ToString("yyyy-MM-dd"),
							"' and RelationTime<='",
							dateTime2.ToString("yyyy-MM-dd"),
							"')"
						});
					}
				}
				else
				{
					text += " and (DATEDIFF(DD,RelationTime,GETDATE())=0)";
				}
			}
			this.linkHead = this.linkHead + "&RelationTime=" + text2;
		}
		string text5 = Fetch.Get("BeginTime");
		if (!string.IsNullOrEmpty(text5))
		{
			this.txtBeginTime.Text = text5;
		}
		else
		{
			text5 = this.txtBeginTime.Text;
		}
		if (!string.IsNullOrEmpty(text5))
		{
			text = text + " and DATEDIFF(DD, RelationTime,'" + text5 + "')<=0";
			this.linkHead = this.linkHead + "&BeginTime=" + text5;
		}
		string text6 = Fetch.Get("EndTime");
		if (!string.IsNullOrEmpty(text6))
		{
			this.txtEndTime.Text = text6;
		}
		else
		{
			text6 = this.txtEndTime.Text;
		}
		if (!string.IsNullOrEmpty(text6))
		{
			if (!string.IsNullOrEmpty(text5))
			{
				if (Convert.ToDateTime(text5) > Convert.ToDateTime(text6))
				{
					this.WriteErrMsg("", Language.Get("Common/DateTimeErr"), "");
				}
			}
			text = text + "and DATEDIFF(DD, RelationTime,'" + text6 + "')>=0";
			this.linkHead = this.linkHead + "&EndTime=" + text6;
		}
		string text7 = Fetch.Get("KeHuName");
		if (!string.IsNullOrEmpty(text7))
		{
			this.txtSearchClientName.Text = text7;
		}
		else
		{
			text7 = this.txtSearchClientName.Text;
		}
		if (!string.IsNullOrEmpty(text7))
		{
			text = text + " and c.ClientName like '%" + text7 + "%'";
			this.linkHead = this.linkHead + "&KeHuName=" + text7;
		}
		string text8 = Fetch.Get("RelationType");
		if (!string.IsNullOrEmpty(text8))
		{
			this.ddlSearchRelationType.ddlSelect(text8);
		}
		else
		{
			text8 = this.ddlSearchRelationType.SelectedValue.Trim();
		}
		if (!string.IsNullOrEmpty(text8))
		{
			text = text + " and RelationType='" + text8 + "'";
			this.linkHead = this.linkHead + "&RelationType=" + text8;
		}
		return text;
	}
	private string GetQueryByOrder()
	{
		string result = "order by ca.ClientAnnalID desc";
		string value = this.ddlDate.SelectedValue.Trim();
		if (!string.IsNullOrEmpty(value))
		{
			result = "order by ca.RelationTime desc";
		}
		string value2 = this.txtBeginTime.Text.Trim();
		string value3 = this.txtEndTime.Text.Trim();
		if (!string.IsNullOrEmpty(value2) || !string.IsNullOrEmpty(value3))
		{
			result = "order by ca.RelationTime desc";
		}
		return result;
	}
	protected void gvManage_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E7EDFE'");
			e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
		}
	}
	protected void gvManage_PageIndexChanging(object sender, GridViewPageEventArgs e)
	{
		this.gvManage.PageIndex = e.NewPageIndex;
		this.BindClientAnnal();
	}
	protected void SeachBut_Click(object sender, EventArgs e)
	{
		this.BindClientAnnal();
	}
}
